package com.hanlink.dsp.model;

import android.content.Intent;

import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.DspSettings;
import com.hanlink.dsp.ca.model.channel.EqChannel;
import com.hanlink.dsp.ca.model.channel.LevelChannel;
import com.hanlink.dsp.ca.model.channel.SummingChannel;
import com.hanlink.dsp.ca.model.channel.XoverChannel;
import com.hanlink.dsp.protocols.DspProtocol;
import com.hanlink.dsp.protocols.XoverProtocol;
import com.hanlink.dsp.util.Constants;
import com.qztech.bluetooth.BluetoothClientManager;

import java.util.List;

import static com.hanlink.dsp.DspContext.BLE_WRITE_DELAY;
import static com.hanlink.dsp.DspContext.BLE_WRITE_DELAY_LARGE;

public class UploadThread extends Thread {


    private void resetDataOnBle()
            throws InterruptedException {
        //eq
        List<EqChannel> eqChannels = DspSettings.EQ_SETTINGS.getEqChannels();
        for (int i = 0; i < eqChannels.size(); i++) {
            eqChannels.get(i).sendAllCmd(false);
            Thread.sleep(BLE_WRITE_DELAY_LARGE);
        }
        //Xover
        List<XoverChannel> xoverChannels = DspSettings.XOVER_SETTINGS.getXoverChannels();
        for (int i = 0, len = xoverChannels.size(); i < len; i++) {
            XoverChannel xoverChannel = xoverChannels.get(i);
            XoverProtocol.send(xoverChannel, xoverChannel.getPassType() == 3 ? 3 : 1, false);
            Thread.sleep(BLE_WRITE_DELAY_LARGE);
            XoverProtocol.send(xoverChannel, xoverChannel.getPassType() == 3 ? 3 : 1, false);
            Thread.sleep(BLE_WRITE_DELAY_LARGE);
            XoverProtocol.send(xoverChannel, xoverChannel.getPassType() == 4 ? 4 : 2, false);
            Thread.sleep(BLE_WRITE_DELAY_LARGE);
            XoverProtocol.send(xoverChannel, xoverChannel.getPassType() == 4 ? 4 : 2, false);
            Thread.sleep(BLE_WRITE_DELAY_LARGE);
        }

        //Advance
        List<LevelChannel> levelChannels = DspSettings.ADVANCE_SETTINGS.getChannel();
        for (int i = 0; i < levelChannels.size(); i++) {
            LevelChannel levelChannel = levelChannels.get(i);
            levelChannel.sendCmdUpload();
            if (i % 5 == 0) {
                Thread.sleep(BLE_WRITE_DELAY_LARGE);
            } else {
                Thread.sleep(BLE_WRITE_DELAY);
            }
        }
        //Summing
        List<SummingChannel> summingChannels = DspSettings.SUMMING_SETTINGS.getSummingChannels();
        for (SummingChannel summingChannel : summingChannels) {
            summingChannel.writeCmd(summingChannel.getCmd(true));
            Thread.sleep(BLE_WRITE_DELAY);
        }
        DspProtocol.setDelay(DspSettings.SUMMING_SETTINGS.getDelay());

        DspSettings.ADVANCE_SETTINGS.sendAllTimeDelayCmd();
        Thread.sleep(BLE_WRITE_DELAY_LARGE);
        DspSettings.ADVANCE_SETTINGS.sendTotalVolCmd(DspSettings.ADVANCE_SETTINGS.getTotalVol(), DspSettings.ADVANCE_SETTINGS.getMute() == 1);
    }

    @Override
    public void run() {
        if (!BluetoothClientManager.getInstance(BtDspApplication.getInstance()).isConnect()) {
            return;
        }
        try {
            Intent startUpload = new Intent(Constants.ACTION_START_UPLOAD);
            BtDspApplication.getInstance().sendBroadcast(startUpload);
//            resetDataOnBle();
            resetDataOnBle();
        } catch (InterruptedException localInterruptedException) {
            localInterruptedException.printStackTrace();
        }
        Intent stopUpload = new Intent(Constants.ACTION_STOP_UPLOAD);
        BtDspApplication.getInstance().sendBroadcast(stopUpload);
    }
}
